<?php
/**
 * @file userdynamics.php
 * @author YueBin
 * @date 2016-04-28 15:10:41
 * @brief 
 */
namespace haibao\user\business;

use haibao\classlibrary\Config;
use haibao\frame\data\query\Condition;
use haibao\app\business\BusinessException;
use \haibao\user\model\data\UserDynamics as UserDynamicsModel;
use \haibao\user\model\filter\UserDynamics as UserDynamicsFilter;
use \haibao\www\model\data\Favorite as FavoriteModel;
use \haibao\user\data\UserDynamics as UserDynamicsData;

class UserDynamics extends \haibao\user\business\Base{
    
    /**
     * @var $_instance
     */
    private static $_instance;
    
    /**
     * instance()
     */
    private static function instance(){
        if(!self::$_instance){
            self::$_instance = new \haibao\user\data\UserDynamics();
        }
        return self::$_instance;
    }
    
    /**
     * 获取用户动态
     */
    public static function getUserDynamics($userId, $page = 1, $limit = 10){
        return self::instance()->getUserDynamics($userId, $page, $limit);
    }
    
    /**
     * 与我相关的动态
     */
    public static function getMyDynamics($userId, $page = 1, $limit = 10){
        $query = new \haibao\www\model\filter\message\MessageBox();
        $query->select(array(
            $query::CONDITION_FIELD_NAME_ID,
            $query::CONDITION_FIELD_NAME_FROM_USER_ID,
            $query::CONDITION_FIELD_NAME_TO_USER_ID,
            $query::CONDITION_FIELD_NAME_MESSAGE_DATA,
            $query::CONDITION_FIELD_NAME_IS_READ,
            $query::CONDITION_FIELD_NAME_CREATETIME
        ))
        ->where($query::CONDITION_FIELD_NAME_TO_USER_ID, Condition::CONDITION_EQUAL, $userId)
        ->where($query::CONDITION_FIELD_NAME_FROM_USER_ID, Condition::CONDITION_NOTEQUAL, $userId)
        ->where($query::CONDITION_FIELD_NAME_MESSAGE_TYPE, Condition::CONDITION_EQUAL, \haibao\www\model\data\message\MessageBox::MESSAGE_TYPE_COMMENT)
        ->where($query::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\www\model\data\message\MessageBox::STATUS_ENABLE)
        ->order($query::CONDITION_FIELD_NAME_CREATETIME, false)
        ->setPager(new \haibao\frame\data\query\Pager($limit, $page));
    
        $data = new \haibao\www\data\message\MessageBox();
        $dynamicList = $data->getAll($query);
        if($dynamicList){
            $articleIdArr = array();
            foreach($dynamicList as $key=>$item){
                $messageData = json_decode($item->MessageData, true);
                if(isset($messageData['MessageType']) && in_array($messageData['MessageType'], array(
                    \haibao\www\business\message\MessageBox::MESSAGE_DATA_MESSAGE_TYPE_REPLY_COMMENT,
                    \haibao\www\business\message\MessageBox::MESSAGE_DATA_MESSAGE_TYPE_PARISE_COMMENT,
                    \haibao\www\business\message\MessageBox::MESSAGE_DATA_MESSAGE_TYPE_ATTENTION_USER,
                ))){
                    $dynamicList[$key]->MessageData = $messageData;
                }else{
                    unset($dynamicList[$key]);
                }
            }
            $data = new \haibao\www\data\message\MessageBox();
            $data->updateMessageReadStatusByUserId((int)$userId, \haibao\www\model\data\message\MessageBox::MESSAGE_TYPE_COMMENT, \haibao\www\model\data\message\MessageBox::READ_HAVE);
            
            $messageUser = \haibao\www\business\message\MessageUser::getMessageUser();
            \haibao\www\business\message\MessageUser::updateMessageUser((int)$userId, \haibao\www\model\data\message\MessageBox::MESSAGE_TYPE_COMMENT,true,0-$messageUser->UnReadCommentCount);
        }
        return array($dynamicList,$query->getPager());
    }
    
    /**
     * 获取好友最新动态
     */
    public static function getNewestDynamics($userId, $page = 1, $limit = 10){
        $query = new \haibao\www\model\filter\message\MessageBox();
        $query->select(array(
            $query::CONDITION_FIELD_NAME_ID,
            $query::CONDITION_FIELD_NAME_FROM_USER_ID,
            $query::CONDITION_FIELD_NAME_TO_USER_ID,
            $query::CONDITION_FIELD_NAME_MESSAGE_DATA,
            $query::CONDITION_FIELD_NAME_IS_READ,
            $query::CONDITION_FIELD_NAME_CREATETIME,
            $query::CONDITION_FIELD_NAME_MESSAGE_TYPE,
        ))
        ->where($query::CONDITION_FIELD_NAME_MESSAGE_TYPE, Condition::CONDITION_EQUAL, \haibao\www\model\data\message\MessageBox::MESSAGE_TYPE_PRIVATE)
        ->where($query::CONDITION_FIELD_NAME_TO_USER_ID, Condition::CONDITION_EQUAL, (int)$userId)
        ->where($query::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\www\model\data\message\MessageBox::STATUS_ENABLE)
        ->order($query::CONDITION_FIELD_NAME_CREATETIME, false)
        ->setPager(new \haibao\frame\data\query\Pager($limit, $page));
        $data = new \haibao\www\data\message\MessageBox();
        $dynamicList = $data->getAll($query);
        $dynamicList = self::buildReturnDynamicData($userId, $dynamicList);
        
        if($dynamicList){
            foreach($dynamicList as $key=>$item){
                $dynamicList[$key]['OperationType'] = self::getOperationType($item['MessageData']['MsgType'], $item['MessageData']['InfoType']);
            }
        }
        return array(
            'data' => $dynamicList,
            'page' => $query->getPager()
        );
    }
    
    /**
     * 添加新动态
     * @param array $dataArray 动态数据
     * @param array $relationData 跟动态数据相关的数据
     */
	public static function addUserDynamics($dataArray,$relationData = array()){
		if(!isset($dataArray['MsgContent'])){
			$msgContent = self::packageMsgContent($dataArray,$relationData);
		}else{
			$msgContent = json_encode($dataArray['MsgContent']);
		}
		if($msgContent && isset($dataArray['UserId'])){
			$userDynamicsModel = new UserDynamicsModel;
			$userDynamicsModel->MsgContent = $msgContent;
			$userDynamicsModel->UserId = $dataArray['UserId'];
			$userDynamicsModel->CreateTime = new \DateTime();
			self::instance()->add($userDynamicsModel);
		};
	}
	
	/**
	 * 我的消息 -> 评论和赞
	 */
	public static function getCommentAndPraise($userId, $isReceive = true, $page = 1, $limit = 10){
	    $query = new \haibao\www\model\filter\message\MessageBox();
	    $query->select(array(
	        $query::CONDITION_FIELD_NAME_ID,
	        $query::CONDITION_FIELD_NAME_FROM_USER_ID,
	        $query::CONDITION_FIELD_NAME_TO_USER_ID,
	        $query::CONDITION_FIELD_NAME_MESSAGE_TYPE,
	        $query::CONDITION_FIELD_NAME_MESSAGE_DATA,
	        $query::CONDITION_FIELD_NAME_IS_READ,
	        $query::CONDITION_FIELD_NAME_CREATETIME,
	    ))
        ->where($isReceive ? $query::CONDITION_FIELD_NAME_TO_USER_ID : $query::CONDITION_FIELD_NAME_FROM_USER_ID, Condition::CONDITION_EQUAL, $userId)
	    ->where($query::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\www\model\data\message\MessageBox::STATUS_ENABLE)
	    ->where($query::CONDITION_FIELD_NAME_MESSAGE_DATA, Condition::CONDITION_LIKE, '%CommentsTypeCode%')
	    ->where($query::CONDITION_FIELD_NAME_MESSAGE_TYPE, Condition::CONDITION_EQUAL, \haibao\www\model\data\message\MessageBox::MESSAGE_TYPE_COMMENT)
	    ->order($query::CONDITION_FIELD_NAME_CREATETIME, false)
	    ->setPager(new \haibao\frame\data\query\Pager($limit, $page));
	    if($isReceive){
	        $query->where($query::CONDITION_FIELD_NAME_FROM_USER_ID, Condition::CONDITION_NOTEQUAL, $userId);
	    }
	    
	    $data = new \haibao\www\data\message\MessageBox();
	    $dataList = $data->getAll($query);
	    $retList = array();
	    if($dataList){
	        $userArray = array();
	        foreach($dataList as $key=>$comment){
	            $id = $comment->Id;
	            $comment = get_object_vars($comment);
	            $comment['Id'] = $id;
                $comment['MessageData'] = json_decode($comment['MessageData'], true);
	            unset($comment['Status'], $comment['Content']);
	            array_push($retList, $comment);
	            array_push($userArray, $comment['FromUserId'], $comment['ToUserId']);
	        }
	        if($userArray){
	            $nicknameArray = \haibao\user\business\User::getNickNameByUserIds($userArray);
	        }
	        if($retList){
	            $dataArr = array();
    	        foreach($retList as $key=>$item){
    	            $retList[$key]['FromNickname'] = array_key_exists($item['FromUserId'], $nicknameArray) ? $nicknameArray[$item['FromUserId']] : '游客';
    	            $retList[$key]['ToNickname'] = array_key_exists($item['ToUserId'], $nicknameArray) ? $nicknameArray[$item['ToUserId']] : '游客';
    	            $retList[$key]['FromAvatar'] = self::getAvatarById($item['FromUserId']);
    	            
    	            if(!isset($dataArr[$item['MessageData']['CommentsTypeCode']])) {
    	                $dataArr[$item['MessageData']['CommentsTypeCode']] = array();
    	            }
    	            array_push($dataArr[$item['MessageData']['CommentsTypeCode']], $item['MessageData']['ObjectId']);
    	        }
    	        
    	        // 修改消息状态未已读
	        	$data = new \haibao\www\data\message\MessageBox();
	        	$data->updateMessageReadStatusByUserId((int)$userId, \haibao\www\model\data\message\MessageBox::MESSAGE_TYPE_COMMENT, \haibao\www\model\data\message\MessageBox::READ_HAVE);
	        	
	        	$messageUser = \haibao\www\business\message\MessageUser::getMessageUser();
	            \haibao\www\business\message\MessageUser::updateMessageUser((int)$userId, \haibao\www\model\data\message\MessageBox::MESSAGE_TYPE_COMMENT,true,0-$messageUser->UnReadCommentCount);
    	        $dataInfoArray = self::getDataInfoArray($dataArr);
    	        foreach($retList as $key=>$item){
	                $retList[$key]['MessageData']['Title'] = isset($dataInfoArray[$item['MessageData']['CommentsTypeCode']][$item['MessageData']['ObjectId']]) ? $dataInfoArray[$item['MessageData']['CommentsTypeCode']][$item['MessageData']['ObjectId']]['Title'] : null;
	                $retList[$key]['MessageData']['Url'] = isset($dataInfoArray[$item['MessageData']['CommentsTypeCode']][$item['MessageData']['ObjectId']]) ? $dataInfoArray[$item['MessageData']['CommentsTypeCode']][$item['MessageData']['ObjectId']]['Url'] : null;
    	            switch($item['MessageData']['CommentsTypeCode']){
    	               case \haibao\comment\model\data\CommentType::TYPE_CODE_ARTICLE:
    	                   $retList[$key]['MessageData']['OperationType'] = '文章';
    	                   break;
    	               case \haibao\comment\model\data\CommentType::TYPE_CODE_PIC:
    	                   $retList[$key]['MessageData']['OperationType'] = '图集';
    	                   break;
    	               case \haibao\comment\model\data\CommentType::TYPE_CODE_FEATURE:
    	                   $retList[$key]['MessageData']['OperationType'] = '专题';
    	                   break;
    	               case \haibao\comment\model\data\CommentType::TYPE_CODE_VIDEO:
    	                   $retList[$key]['MessageData']['OperationType'] = '视频';
    	                   break;
    	               case \haibao\comment\model\data\CommentType::TYPE_CODE_BRAND:
    	                   $retList[$key]['MessageData']['OperationType'] = '品牌';
    	                   break;
    	               case \haibao\comment\model\data\CommentType::TYPE_CODE_STAR:
    	                   $retList[$key]['MessageData']['OperationType'] = '明星';
    	                   break;
    	               case \haibao\comment\model\data\CommentType::TYPE_CODE_DESIGNER:
    	                   $retList[$key]['MessageData']['OperationType'] = '设计师';
    	                   break;
    	               case \haibao\comment\model\data\CommentType::TYPE_CODE_STAR_PK:
    	                   $retList[$key]['MessageData']['OperationType'] = '明星造型PK台';
    	                   break;
    	               case \haibao\comment\model\data\CommentType::TYPE_CODE_HB_HOME_BLOG:
    	                   $retList[$key]['MessageData']['OperationType'] = '博文';
    	                   break;
    	               case \haibao\comment\model\data\CommentType::TYPE_CODE_MEIJU_ARTICLE:
    	                   $retList[$key]['MessageData']['OperationType'] = '美剧文章';
    	                   break;
    	               case \haibao\comment\model\data\CommentType::TYPE_CODE_MEIJU_INFO:
    	                   $retList[$key]['MessageData']['OperationType'] = '美剧';
    	                   break;
	                   case \haibao\comment\model\data\CommentType::TYPE_CODE_IMAGE_LIBRARY:
	                       $retList[$key]['MessageData']['OperationType'] = '图片库';
	                       break;
                       case \haibao\comment\model\data\CommentType::TYPE_CODE_FASHION_WEAR:
                           $retList[$key]['MessageData']['OperationType'] = '时尚穿搭';
                           break;
    	               default:
    	                   $retList[$key]['MessageData']['OperationType'] = '文章';
    	                   break;
    	            }
    	        }
	        }
	    }
	    return array(
	        'data' => $retList,
	        'page' => $query->getPager()
	    );
	}

	/**
	 * 我的消息 -> 系统消息
	 */
	public static function getSystemMessage($userId, $page = 1, $limit = 10){
	    $query = new \haibao\www\model\filter\message\MessageBox();
	    $query->select(array(
	        $query::CONDITION_FIELD_NAME_ID,
	        $query::CONDITION_FIELD_NAME_FROM_USER_ID,
	        $query::CONDITION_FIELD_NAME_TO_USER_ID,
	        $query::CONDITION_FIELD_NAME_MESSAGE_TYPE,
	        $query::CONDITION_FIELD_NAME_MESSAGE_DATA,
	        $query::CONDITION_FIELD_NAME_IS_READ,
	        $query::CONDITION_FIELD_NAME_CREATETIME,
	    ))
	    ->where($query::CONDITION_FIELD_NAME_TO_USER_ID, Condition::CONDITION_EQUAL, $userId)
	    ->where($query::CONDITION_FIELD_NAME_STATUS, Condition::CONDITION_EQUAL, \haibao\www\model\data\message\MessageBox::STATUS_ENABLE)
	    ->where($query::CONDITION_FIELD_NAME_MESSAGE_TYPE, Condition::CONDITION_EQUAL, \haibao\www\model\data\message\MessageBox::MESSAGE_TYPE_SYSTEM)
	    ->order($query::CONDITION_FIELD_NAME_CREATETIME, false)
	    ->setPager(new \haibao\frame\data\query\Pager($limit, $page));
	     
	    $data = new \haibao\www\data\message\MessageBox();
	    $dataList = $data->getAll($query);
	    
	    if($dataList){
	        $messageIdArr = array();
	        foreach($dataList as $key=>$item){
	            $dataList[$key]->MessageData = json_decode($item->MessageData, true);
	            if($item->IsRead == \haibao\www\model\data\message\MessageBox::READ_NOT){
	                array_push($messageIdArr, $item->Id);
	            }
	        }
	        if($messageIdArr){
	        	$data = new \haibao\www\data\message\MessageBox();
	        	$data->updateMessageReadStatusByUserId((int)$userId, \haibao\www\model\data\message\MessageBox::MESSAGE_TYPE_SYSTEM, \haibao\www\model\data\message\MessageBox::READ_HAVE);
	        	
	        	$messageUser = \haibao\www\business\message\MessageUser::getMessageUser();
	            \haibao\www\business\message\MessageUser::updateMessageUser((int)$userId, $item->MessageType,true,0-$messageUser->UnReadSystemCount);
	        }
	    }
	    
	    return array(
	        'data' => $dataList,
	        'page' => $query->getPager()
	    );
	}
	
	private static function packageMsgContent($dataArray,$relationData){
		$msgType = $dataArray['MsgType'];
		$infoType = $dataArray['InfoType'];
		$infoId = $dataArray['InfoId'];
		$infoUrl = $title = $fromInfoUrl = $fromTitle = $key = '';
		if(isset($dataArray['MsgContent'])){
			
		}else{
			switch($msgType){
				case UserDynamicsModel::MESSAGE_TYPE_ATTENTION:
					if($infoType == FavoriteModel::ATTENTION_TYPE_STAR){
						$starInfo = \haibao\cms\business\Celebrity::getStarByIds(array((int)$infoId));
						if(isset($starInfo[$infoId])){
							$title = $starInfo[$infoId]->NameCn;
							$infoUrl = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_STARS).'/star/'.$starInfo[$infoId]->TopicId.'/';
						}
					}elseif($infoType == FavoriteModel::ATTENTION_TYPE_BRAND){
						$brandInfo = \haibao\cms\business\Brand::getBrandByIds(array((int)$infoId));
						if(isset($brandInfo[$infoId])){
							$title = $brandInfo[$infoId]->NameCN;
							$infoUrl = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_BRANDS).'/brand/'.$brandInfo[$infoId]->BrandUrl.'/';
						}
						
					}elseif($infoType == FavoriteModel::ATTENTION_TYPE_BLOGGER){
						$title = $relationData['UserName'];
						$infoUrl = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_BOZHU).'/user/'.$infoId.'/';
					}elseif($infoType == FavoriteModel::ATTENTION_TYPE_DESIGNER){
						
						$designerInfo = \haibao\cms\business\Designer::getDesignerByIds($infoId);
						if(isset($designerInfo[$infoId])){
							$title = $designerInfo[$infoId]->NameCN;
							$infoUrl = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_DESIGNER).'/designer/'.$designerInfo[$infoId]->TopicId.'/';
						}
						
					}elseif($infoType == FavoriteModel::ATTENTION_TYPE_AMERICAN){
						$filter = new \haibao\cms\model\filter\americandrama\AmericanDefine();
						$filter->select(array(\haibao\cms\model\filter\americandrama\AmericanDefine::CONDITION_FIELD_NAME_NAMECN));
						$filter->where(\haibao\cms\model\filter\americandrama\AmericanDefine::CONDITION_FIELD_NAME_ID,Condition::CONDITION_EQUAL,$infoId);
						$americanInfo = \haibao\cms\business\americandrama\AmericanDefine::getAmericanByFilter($filter);
						if($americanInfo){
							$title = $americanInfo->NameCH;
							$infoUrl = \haibao\classlibrary\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_TV_HOST).'/meiju/'.$infoId.'/';
						}
					}elseif ($infoType == FavoriteModel::ATTENTION_TYPE_USER){
					    $toUserName = \haibao\user\business\User::getUserNickNameByUserIds(array($infoId,$dataArray['UserId']));
					    
					    $fromInfoUrl = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_USER).'/space/'.$dataArray['UserId'].'/';
					    $fromTitle = $toUserName[$dataArray['UserId']];
					    $infoUrl = \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_USER).'/space/'.$infoId.'/';
					    $title = $toUserName[$infoId];
					    $key = '关注了';
					}
					break;
				case UserDynamicsModel::MESSAGE_TYPE_COLLECTION:
					
					
					
					break;
				case UserDynamicsModel::MESSAGE_TYPE_COMMENT:
					break;
				case UserDynamicsModel::MESSAGE_TYPE_APPLY_TRY:
					break;
				case UserDynamicsModel::MESSAGE_TYPE_BLOG:
					break;
				case UserDynamicsModel::MESSAGE_TYPE_BBS:
					break;
				case UserDynamicsModel::MESSAGE_TYPE_COMMENT_PRAISE:
					break;
				default :
					break;
			}
			$msgContent = array(
					'MsgType'=>$msgType,
					'InfoId'=>$infoId,
					'InfoType'=>$infoType,
    			    'fromInfoUrl'=>$fromInfoUrl,
    			    'fromTitle'=>$fromTitle,
					'InfoUrl'=>$infoUrl,
					'Title'=>$title,
			        'key'=>$key,
			);
		}
		
		return json_encode($msgContent);
	}

	public static function getUserDynamicsContent($userDynamicsData){
		
		$packContent = $msgName = '';
		$msgContent = json_decode($userDynamicsData);
		switch($msgContent->MsgType){
			case UserDynamicsModel::MESSAGE_TYPE_ATTENTION:
				$msgName = 'TA关注了';
				if($msgContent->InfoType == FavoriteModel::ATTENTION_TYPE_STAR){
					$typeName = '明星';
				}elseif($msgContent->InfoType == FavoriteModel::ATTENTION_TYPE_BRAND){
					$typeName = '品牌';
				}elseif($msgContent->InfoType == FavoriteModel::ATTENTION_TYPE_BLOGGER){
					$typeName = '博主';
				}elseif($msgContent->InfoType == FavoriteModel::ATTENTION_TYPE_DESIGNER){
					$typeName = '设计师';
				}elseif($msgContent->InfoType == FavoriteModel::ATTENTION_TYPE_AMERICAN){
					$typeName = '美剧';
				}elseif($msgContent->InfoType == FavoriteModel::ATTENTION_TYPE_USER){
					$typeName = '报友';
				}
				$msgName .= $typeName;
				break;
			case UserDynamicsModel::MESSAGE_TYPE_COLLECTION:

				$msgName = 'TA收藏了';
				if($msgContent->InfoType == FavoriteModel::ID_TYPE_AID_NUM){
					$typeName = '文章';
				}elseif($msgContent->InfoType == FavoriteModel::ID_TYPE_PID_NUM){
					$typeName = '图集';
				}elseif($msgContent->InfoType == FavoriteModel::ID_TYPE_SUBJECT_NUM){
					$typeName = '专题';
				}elseif($msgContent->InfoType == FavoriteModel::ID_TYPE_VIDEO_NUM){
					$typeName = '视频';
				}elseif($msgContent->InfoType == FavoriteModel::ID_TYPE_TRY){
					$typeName = '试用';
				}elseif($msgContent->InfoType == FavoriteModel::ID_TYPE_THREAD_NUM){
					$typeName = '帖子';
				}elseif($msgContent->InfoType == FavoriteModel::ID_TYPE_BLOG_NUM){
					$typeName = '博客';
				}
				$msgName .= $typeName;
				break;
			case UserDynamicsModel::MESSAGE_TYPE_COMMENT:
				$msgName = 'TA评论了';
				break;
			case UserDynamicsModel::MESSAGE_TYPE_APPLY_TRY:
				$msgName = 'TA申请了试用';
				break;
			case UserDynamicsModel::MESSAGE_TYPE_BLOG:
				$msgName = 'TA发布了博文';
				break;
			case UserDynamicsModel::MESSAGE_TYPE_BBS:
				$msgName = 'TA发布了帖子';
				break;
			case UserDynamicsModel::MESSAGE_TYPE_COMMENT_PRAISE:
				$msgName = 'TA赞了';
				break;
			default :
				break;
		}

		$packContent .= self::renderHtml('span',$msgName,array('class'=>'first'));
		$packContent .= self::renderHtml('span',self::renderA($msgContent->Title, $msgContent->InfoUrl),array('class'=>'tow','target'=>'_blank'));
		return $packContent;
	}
	
	private static function getOperationType($msgType, $infoType = null){
	    $msgName = null;
	    switch($msgType){
	        case UserDynamicsModel::MESSAGE_TYPE_ATTENTION:
	            $msgName = '关注了';
	            if($infoType){
    	            if($infoType == FavoriteModel::ATTENTION_TYPE_STAR){
    	                $typeName = self::renderHtml('span', '明星', array('class'=>'gray'));
    	            }elseif($infoType == FavoriteModel::ATTENTION_TYPE_BRAND){
    	                $typeName = self::renderHtml('span', '品牌', array('class'=>'gray'));
    	            }elseif($infoType == FavoriteModel::ATTENTION_TYPE_BLOGGER){
    	                $typeName = self::renderHtml('span', '博主', array('class'=>'gray'));
    	            }elseif($infoType == FavoriteModel::ATTENTION_TYPE_DESIGNER){
    	                $typeName = self::renderHtml('span', '设计师', array('class'=>'gray'));
    	            }elseif($infoType == FavoriteModel::ATTENTION_TYPE_AMERICAN){
    	                $typeName = self::renderHtml('span', '美剧', array('class'=>'gray'));
    	            }elseif($infoType == FavoriteModel::ATTENTION_TYPE_USER){
    	                $typeName = self::renderHtml('span', '报友', array('class'=>'gray'));
    	            }
    	            $msgName .= $typeName;
	            }
	            break;
	        case UserDynamicsModel::MESSAGE_TYPE_COLLECTION:
	            $msgName = '收藏了';
	            if($infoType){
    	            if($infoType == FavoriteModel::ID_TYPE_AID_NUM){
    	                $typeName = self::renderHtml('span', '文章', array('class'=>'gray'));
    	            }elseif($infoType == FavoriteModel::ID_TYPE_PID_NUM){
    	                $typeName = self::renderHtml('span', '图集', array('class'=>'gray'));
    	            }elseif($infoType == FavoriteModel::ID_TYPE_SUBJECT_NUM){
    	                $typeName = self::renderHtml('span', '专题', array('class'=>'gray'));
    	            }elseif($infoType == FavoriteModel::ID_TYPE_VIDEO_NUM){
    	                $typeName = self::renderHtml('span', '视频', array('class'=>'gray'));
    	            }elseif($infoType == FavoriteModel::ID_TYPE_TRY){
    	                $typeName = self::renderHtml('span', '试用', array('class'=>'gray'));
    	            }elseif($infoType == FavoriteModel::ID_TYPE_THREAD_NUM){
    	                $typeName = self::renderHtml('span', '帖子', array('class'=>'gray'));
    	            }elseif($infoType == FavoriteModel::ID_TYPE_BLOG_NUM){
    	                $typeName = self::renderHtml('span', '博客', array('class'=>'gray'));
    	            }
    	            $msgName .= $typeName;
	            }
	            break;
	        case UserDynamicsModel::MESSAGE_TYPE_COMMENT:
	            $msgName = '评论了';
	            if($infoType){
    	            if($infoType == FavoriteModel::ID_TYPE_AID_NUM){
    	                $typeName = self::renderHtml('span', '文章', array('class'=>'gray'));
    	            }elseif($infoType == FavoriteModel::ID_TYPE_PID_NUM){
    	                $typeName = self::renderHtml('span', '图集', array('class'=>'gray'));
    	            }elseif($infoType == FavoriteModel::ID_TYPE_SUBJECT_NUM){
    	                $typeName = self::renderHtml('span', '专题', array('class'=>'gray'));
    	            }
    	            $msgName .= $typeName;
	            }
	            break;
	        case UserDynamicsModel::MESSAGE_TYPE_APPLY_TRY:
	            $msgName = '申请了试用';
	            break;
	        case UserDynamicsModel::MESSAGE_TYPE_BLOG:
	            $msgName = '发布了博文';
	            break;
	        case UserDynamicsModel::MESSAGE_TYPE_BBS:
	            $msgName = '发布了帖子';
	            break;
	        case UserDynamicsModel::MESSAGE_TYPE_COMMENT_PRAISE:
	            $msgName = '赞了';
	            break;
	        default :
	            break;
	    }
	    return $msgName;
	}
	
	/**
	 * 
	 * @param Object $dynamicList 
	 * @param Boolure $isMessageBox 数据是否为消息盒子数据
	 */
	
	public static function buildReturnDynamicData($userId, $dynamicList,$isMessageBox = false){
	   
        $dynamicArray = $userArray = array();
        foreach($dynamicList as $item){
        	$temp = get_object_vars($item);
        	$temp['Id'] = $item->Id;
            array_push($dynamicArray, $temp);
            array_push($userArray, $item->FromUserId, $item->ToUserId);
        }
        
        $data = new \haibao\www\data\message\MessageBox();
        $data->updateMessageReadStatusByUserId((int)$userId, \haibao\www\model\data\message\MessageBox::MESSAGE_TYPE_DYNAMIC, \haibao\www\model\data\message\MessageBox::READ_HAVE);
         
        $messageUser = \haibao\www\business\message\MessageUser::getMessageUser();
        \haibao\www\business\message\MessageUser::updateMessageUser((int)$userId, \haibao\www\model\data\message\MessageBox::MESSAGE_TYPE_PRIVATE,true,0-$messageUser->UnReadPrivateCount);
        
        if($userArray){
            $nicknameArray = \haibao\user\business\User::getNickNameByUserIds($userArray);
        }
        
        foreach($dynamicArray as $key=>$item){
            $dynamicArray[$key]['FromNickname'] = array_key_exists($item['FromUserId'], $nicknameArray) ? $nicknameArray[$item['FromUserId']] : null;
            $dynamicArray[$key]['FromUserAvatar'] = self::getAvatarById($item['FromUserId']);
            $dynamicArray[$key]['ToNickname'] = array_key_exists($item['ToUserId'], $nicknameArray) ? $nicknameArray[$item['ToUserId']] : null;
            if(!$isMessageBox){
                $dynamicArray[$key]['MessageData'] = json_decode($item['MessageData'], true);
            }else {
                $dynamicArray[$key]['MessageData'] = $item['MessageData'];
            }
            
        }
      
	    return $dynamicArray;
	}
	
	/**
	 * 解析动态json数据为html格式数据展示
	 */
	private static function parseDynamicData($dataArray){
	    $fromUserId = $dataArray['FromUserId'];
	    $fromNickname = $dataArray['FromNickname'];
	    $touUserId = array_key_exists('ToUserId', $dataArray) ? $dataArray['ToUserId'] : null;
	    $toNickname = array_key_exists('ToNickname', $dataArray) ? $dataArray['ToNickname'] : null;
	    if(!is_object($dataArray['MessageData'])){
	    	$msgData = json_decode($dataArray['MessageData']);
	    }else{
	    	$msgData = $dataArray['MessageData'];
	    }
	    
	    $html = '<span class="first">';
	    $html .= self::renderA($fromNickname, self::getUserProfileUrl($fromUserId));
	    $html .= self::convertHtml(get_object_vars($msgData));
	    return $html;
	}
	
	private static function convertHtml($data, $who = 'TA'){
	    $infoId = $data['InfoId'];
	    $infoType = $data['InfoType'];
	    $title = $data['Title'];
	    $infoUrl = $data['InfoUrl'];
	    
	    $html = '';
	    switch ($data['MsgType']){
	        case UserDynamicsModel::MESSAGE_TYPE_ATTENTION:
	            $html .= self::renderHtml('span', $who.'关注了', array('class="first"'));
	            break;
	        case UserDynamicsModel::MESSAGE_TYPE_COLLECTION:
	            $html .= self::renderHtml('span', $who.'收藏了', array('class="first"'));
	            if($infoType == FavoriteModel::ID_TYPE_AID_NUM){
	                $html .= '文章';
	            }elseif($infoType == FavoriteModel::ID_TYPE_PID_NUM){
	                $html .= '图集';
	            }elseif($infoType == FavoriteModel::ID_TYPE_SUBJECT_NUM){
	                $html .= '专题';
	            }elseif($infoType == FavoriteModel::ID_TYPE_VIDEO_NUM){
	                $html .= '视频';
	            }elseif($infoType == FavoriteModel::ID_TYPE_TRY){
	                $html .= '试用';
	            }elseif($infoType == FavoriteModel::ID_TYPE_THREAD_NUM){
	                $html .= '帖子';
	            }elseif($infoType == FavoriteModel::ID_TYPE_BLOG_NUM){
	                $html .= '博客';
	            }
	            break;
	        case UserDynamicsModel::MESSAGE_TYPE_COMMENT:
	            $html .= self::renderHtml('span', $who.'评论了', array('class="first"'));
	            break;
	        case UserDynamicsModel::MESSAGE_TYPE_APPLY_TRY:
	            $html .= self::renderHtml('span', $who.'申请了试用', array('class="first"'));
	            break;
	        case UserDynamicsModel::MESSAGE_TYPE_BLOG:
	            $html .= self::renderHtml('span', $who.'发布了博文', array('class="first"'));
	            break;
	        case UserDynamicsModel::MESSAGE_TYPE_BBS:
	            $html .= self::renderHtml('span', $who.'发布了帖子', array('class="first"'));
	            break;
	        case UserDynamicsModel::MESSAGE_TYPE_COMMENT_PRAISE:
	            $html .= self::renderHtml('span', $who.'赞了', array('class="first"'));
	            break;
	        default :
	            break;
	    }
        $html .= self::renderHtml('span', self::renderA($data['Title'], $data['InfoUrl']), array('class'=>'tow'));
	    return $html;
	}
	
	private static function getDataInfoArray($dataArr){
	    $retArr = array();
	    $key = \haibao\comment\model\data\CommentType::TYPE_CODE_ARTICLE;
	    if(isset($dataArr[$key])){
	        $retArr[$key] = \haibao\www\business\message\MessageBox::getArtcileList($dataArr[$key]);
	    }
	    $key = \haibao\comment\model\data\CommentType::TYPE_CODE_PIC;
	    if(isset($dataArr[$key])){
	        $retArr[$key] = \haibao\www\business\message\MessageBox::getImageCollList($dataArr[$key]);
	    }
	    $key = \haibao\comment\model\data\CommentType::TYPE_CODE_FEATURE;
	    if(isset($dataArr[$key])){
	        $featureIds = array_filter(array_unique($dataArr[$key]));
	        $featureList = \haibao\cms\business\FeaturePublish::getFeatureByIds($featureIds);
	        foreach($featureList as $list){
	            //$retArr[$key][$list->Id] = $list;
	            $retArr[$key][$list->Id]['Title'] = $list->FeatureDefine->FeatureName;
	            $retArr[$key][$list->Id]['Url'] = $list->Url;
	        }
	    }
	    $key = \haibao\comment\model\data\CommentType::TYPE_CODE_VIDEO;
	    if(isset($dataArr[$key])){
	        $videoIds = array_filter(array_unique($dataArr[$key]));
	        $retArr[$key] = \haibao\cms\business\Video::getVideoByids($videoIds,true);
	    }
	    $key = \haibao\comment\model\data\CommentType::TYPE_CODE_BRAND;
	    if(isset($dataArr[$key])){
	        $brandIds = array_filter(array_unique($dataArr[$key]));
	        $retArr[$key] = \haibao\cms\business\Brand::getBrandByIds($brandIds,true);
	    }
	    $key = \haibao\comment\model\data\CommentType::TYPE_CODE_STAR;
	    if(isset($dataArr[$key])){
	        $starIds = array_filter(array_unique($dataArr[$key]));
	        $retArr[$key] = \haibao\cms\business\Celebrity::getStarByIds($starIds,true);
	    }
	    $key = \haibao\comment\model\data\CommentType::TYPE_CODE_DESIGNER;
	    if(isset($dataArr[$key])){
	        $designerIds = array_flip(array_flip($dataArr[$key]));
	        $retArr[$key] = \haibao\cms\business\Designer::getDesignerByIds($designerIds,null,true);
	    }
	    $key = \haibao\comment\model\data\CommentType::TYPE_CODE_STAR_PK;
	    if(isset($dataArr[$key])){
	        $retArr[$key] = \haibao\www\business\message\MessageBox::getStarPkList($dataArr[$key]);
	    }
	    $key = \haibao\comment\model\data\CommentType::TYPE_CODE_HB_HOME_BLOG;
	    if(isset($dataArr[$key])){
	        $retArr[$key] = \haibao\www\business\message\MessageBox::getBlogList($dataArr[$key]);
	    }
	    $key = \haibao\comment\model\data\CommentType::TYPE_CODE_MEIJU_INFO;
	    if(isset($dataArr[$key])){
	        $retArr[$key] = \haibao\www\business\message\MessageBox::getAmericanList($dataArr[$key]);
	    }
	    $key = \haibao\comment\model\data\CommentType::TYPE_CODE_IMAGE_LIBRARY;
	    if(isset($dataArr[$key])){
	        $retArr[$key] = \haibao\www\business\message\MessageBox::getImageLibraryList($dataArr[$key]);
	    }
	    $key = \haibao\comment\model\data\CommentType::TYPE_CODE_FASHION_WEAR;
	    if(isset($dataArr[$key])){
	        $retArr[$key] = \haibao\www\business\message\MessageBox::getFashionWearList($dataArr[$key]);
	    }
	    return $retArr;
	}

	/**
	 * 根据用户id获取用户真实头像地址
	 */
	private static function getAvatarById($userId, $size = 'middle'){
	    return \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_BBS).'/uc_server/avatar.php?uid='.$userId.'&size='.$size;
	}
	
	private static function getUserProfileUrl($userId){
	    return \haibao\classlibrary\www\Config::getConfig(\haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_HOST_USER).'/u/'.$userId.'.html';
	}
	
	private static function renderA($text, $url, $option = array()){
	    return '<a href="'.$url.'" target="_blank" '.self::buildOption($option).'>'.$text.'</a>';
	}
	
	private static function renderImage($imageUrl, $option = array()){
	    return '<img src="'.$imageUrl.'" '.self::buildOption($option).' />';
	}

	private static function renderHtml($htmlTag, $htmlContent,$option = array()){
		return '<'.$htmlTag.' '.self::buildOption($option).'>'.$htmlContent.'</'.$htmlTag.'>';
	}	
	private static function buildOption($option = array()){
	    $array = array();
	    if($option){
	        foreach ($option as $name => $value) {
	            array_push($array, $name.'="'.$value.'"');
	        }
	    }
	    return implode(' ', $array);
	}
} 